package DataFile;

import java.nio.ByteBuffer;
import java.util.Formatter;

import ClassData.ConstantPoolUtf8_Info;


public class Structure_CP_UTF8Info extends Structure_CP_Constant {

	ConstantPoolUtf8_Info myUtf8Info;

	public Structure_CP_UTF8Info(ConstantPoolUtf8_Info myUtf8Info) {
		this.myUtf8Info=myUtf8Info;
	}

	protected int setBaseAddress(int newBaseAdress) {
		while ((newBaseAdress%2)!=0) {
			newBaseAdress++;
		}
		baseAdress=newBaseAdress;
		return(baseAdress+getMemorySize());
	}

	public int getMemorySize() {
		int size;
		size =BYTE_WIDTH;
		size+=SHORT_WIDTH;
		size+=myUtf8Info.getSize();
		return(size);	
	}	
	
	protected String getMemoryMapAsString() {
//		String returnString;
//		
//		returnString=getAdressAsHexString(baseAdress);
//		returnString+=" : (utf8 info)   : size - " + new Formatter().format("%3d",getMemorySize());
//		returnString+=+"\n";
		
		Formatter fmt = new Formatter();
		return "" + fmt.format("%s |%14s | size - %3d | %s\n", 
				getAdressAsHexString(baseAdress), 
				"(utf8 info)", 
				getMemorySize(),
				"value - " + myUtf8Info.getString());
	}

	public String getString() {
		return(myUtf8Info.getString());	
	}

	/**
	 *  writes data into memory file
	 *  
	 *  @author Tavin
	 *  
	 *  @param fileData buffer for data 
	 */
	public void writeData(ByteBuffer fileData) {
		byte [] myUtf8String;
		int counter;
		
		fileData.position(baseAdress-DataFileCreator.BASE_MEMORY_ADRESS);
		
		myUtf8String=myUtf8Info.getUtf8String();
	
		fileData.put((byte)myUtf8Info.getType());
		fileData.putShort((short)myUtf8String.length);
		
		for (counter=0;counter<myUtf8String.length;counter++) {
			fileData.put(myUtf8String[counter]);
		}
		
	}

}